package com.yln.watcher.client;

import com.yln.watcher.AssignedTaskWatcher;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.data.Stat;

@Slf4j
public class CreateAssignWorkerCallback implements AsyncCallback.Create2Callback {
    @Override
    public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
        ZkClient client = (ZkClient) ctx;
        KeeperException.Code code = KeeperException.Code.get(rc);
        switch (code) {
            case OK:
                // 创建/assign/{worker-id}节点成功
                log.info("create {} success. add watcher finished", path);
                client.addWatcher(path, new AssignedTaskWatcher(client));
                break;
            case NODEEXISTS:
                // 存在/assign/{worker-id}节点
                log.info("exist {}. add watcher finished", path);
                client.addWatcher(path, new AssignedTaskWatcher(client));
                break;
            default:
                ;

        }
    }
}
